package logic;
import java.net.DatagramPacket;
import java.net.InetAddress;

public class ThreadMC extends Thread{
	private Server server;
	public static char CR = '\r';
	public static char LF = '\n';
	private final String defautVersion="1.0";
	public ThreadMC(Server server)
	{
		this.server=server;
	}
	public void run(){
		


		Thread deletedThread = new Thread(new ThreadDeletedMessages(server));


		while(true){
			byte[] receiveData = new byte[65000];
			try {
				DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
				server.getMC_socket().receive(receivePacket);

				String message_received = new String( receivePacket.getData(),0,receivePacket.getLength());
				String[] chunkParts=new String[10];
				InetAddress IPAddress = receivePacket.getAddress();
				int indexFinalHeader = message_received.indexOf("\r\n\r\n");
				if(indexFinalHeader!= -1)
				{
					String header = message_received.substring(0, indexFinalHeader);
					chunkParts=header.split(" ");
					if(chunkParts[0].equals("STORED")&&chunkParts[1].equals(defautVersion))
					{
						LogMensage lm=new LogMensage("RECEIVED STORED - "+"HOST: " + IPAddress.getHostName()+ " FileID: "+chunkParts[2] + " Chunk number: "+ chunkParts[3]);
						Server.logMensagesStr.insertElementAt(lm.str(),0);
						StoredMessage storedMessage = new StoredMessage(chunkParts[2],Integer.parseInt(chunkParts[3]),receivePacket.getAddress().toString());
						server.getStoredMessages().add(storedMessage);

					}
					else if(chunkParts[0].equals("DELETE"))
					{
						LogMensage lm=new LogMensage("RECEIVED DELETE - "+"HOST: " + IPAddress.getHostName()+ " FileID: "+chunkParts[1]);
						Server.logMensagesStr.insertElementAt(lm.str(),0);
						Thread t = new Thread(new ThreadDelete(server,chunkParts));
						t.start();
					}
					else if(chunkParts[0].equals("DELETED"))
					{
						LogMensage lm=new LogMensage("RECEIVED DELETE REPLY - "+"HOST: " + IPAddress.getHostName()+" FileID: "+chunkParts[1]);
						Server.logMensagesStr.insertElementAt(lm.str(),0);
						DeletedMessage deletedMessage = new DeletedMessage(chunkParts[1], receivePacket.getAddress().toString());
						server.getDeletedMessages().add(deletedMessage);
						if(!deletedThread.isAlive()) //se a thread nao estiver em execu��o ent�o recome�a se a sua execu��o
							deletedThread.start();
					}
					else if(chunkParts[0].equals("GETCHUNK"))
					{
						LogMensage lm=new LogMensage("RECEIVED GETCHUNK - "+"HOST: " + IPAddress.getHostName()+ " FileID: "+chunkParts[2] + " Chunk number: "+ chunkParts[3]);
						Server.logMensagesStr.insertElementAt(lm.str(),0);
						if(chunkParts[1].equals(defautVersion))
						{
							Thread t = new Thread(new ThreadGetchunk(server,chunkParts));
							t.start();
						}
						else if(chunkParts[1].equals(Server.VERSION) )
						{
							Thread t = new Thread(new ThreadGetchunk(server,chunkParts,IPAddress));
							t.start();
						}
					}
					else if(chunkParts[0].equals("REMOVED")&&chunkParts[1].equals(defautVersion))
					{
						LogMensage lm=new LogMensage("RECEIVED REMOVED - HOST: " + IPAddress.getHostName()+ " FileID: "+chunkParts[2] + " Chunk number: "+ chunkParts[3]);
						Server.logMensagesStr.insertElementAt(lm.str(),0);
						Thread t = new Thread(new ThreadRemoved(server,receivePacket));
						t.start();

					}
					else if(Server.enhancement && chunkParts[0].equals("HELLO"))
					{
						LogMensage lm=new LogMensage("RECEIVED HELLO - The " + IPAddress.getHostName()+ " just connect.");
						Server.logMensagesStr.insertElementAt(lm.str(),0);
						Thread t = new Thread(new ThreadHello(server, receivePacket.getAddress().toString()));
						t.start();
					}

					//	Server.logMensagesStr.add(Server.logMensagesStr.size(), lm.str());
					//	System.out.println("MC ->"+chunkParts[0]);
				}
			} catch (Exception  e) {

				//e.printStackTrace();
			}


		}
	}
	public String getDefautVersion() {
		return defautVersion;
	}

}
